package com.neusoft.crm.controller;

import com.neusoft.crm.po.SaleChance;
import com.neusoft.crm.po.SalePlan;
import com.neusoft.crm.service.ISalePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 客户开发计划前端接口类
 *
 * @author msprc
 */
@RestController
public class SalePlanController {

    @Autowired
    private ISalePlanService salePlanService;

    /**
     * 模糊查询开发计划
     *
     * @param salePlan 所属客户载体：开发机会
     * @return 列表
     */
    @RequestMapping("/selectSalePlanList")
    public List<SalePlan> selectSystemUserList(@RequestBody SalePlan salePlan) {
        return salePlanService.selectSalePlanList(salePlan);
    }

    /**
     * 创建开发计划
     *
     * @param salePlan 要创建的开发计划
     * @return 受影响的行数：1成功，0失败
     */
    @RequestMapping("/createSalePlan")
    public int createSalePlan(@RequestBody SalePlan salePlan) {
        return salePlanService.createSalePlan(salePlan);
    }

    /**
     * 修改开发计划列表
     *
     * @param salePlanList 要修改的开发计划
     * @return 受影响的行数：n成功，0失败
     */
    @RequestMapping("/updateSalePlanList")
    public int updateSalePlanList(@RequestBody List<SalePlan> salePlanList) {
        // 获取已有的销售机会列表
        SaleChance saleChance = new SaleChance();
        saleChance.setChanceId(salePlanList.get(0).getPlanChcId());
        List<SalePlan> alreadyExistSalePlanList = salePlanService.getSalePlanListBySaleChanceId(saleChance);
        // 判断已有的项是否有更新
        int i = 0, influenceLineCount = 0;
        List<SalePlan> updateRequestSalePlanList = new ArrayList<>();
        for (; i < alreadyExistSalePlanList.size(); i++) {
            if (!Objects.equals(salePlanList.get(i).getPlanTodo(), alreadyExistSalePlanList.get(i).getPlanTodo()) ||
                    !Objects.equals(salePlanList.get(i).getPlanChcId(), alreadyExistSalePlanList.get(i).getPlanChcId()) ||
                    !Objects.equals(salePlanList.get(i).getPlanResult(), alreadyExistSalePlanList.get(i).getPlanResult())
            ) {
                // 把要更新的项存入到列表中统一更新，提高效率
                updateRequestSalePlanList.add(salePlanList.get(i));
                influenceLineCount++;
            }
        }
        // 判断更新的行数，对需要更新的记录进行更新
        if (updateRequestSalePlanList.size() != 0) {
            influenceLineCount += salePlanService.updateSalePlanList(updateRequestSalePlanList);
            influenceLineCount--;
        }
        // 判断是否有新增，将if语句外置，降低CPU流水线运行开销，提高运行效率
        if (salePlanList.size() != alreadyExistSalePlanList.size()) {
            influenceLineCount += salePlanService.createSalePlan(salePlanList.get(i));
        }
        return influenceLineCount;
    }


    /**
     * 删除单条开发计划
     *
     * @param salePlan 要删除的开发计划
     * @return 受影响的行数：1成功，0失败
     */
    @RequestMapping("/deleteSalePlan")
    public int deleteSalePlan(@RequestBody SalePlan salePlan) {
        salePlan.setPlanResult("1");
        return salePlanService.deleteSalePlan(salePlan);
    }

    /**
     * 根据chanceId找到销售机会对应的开发计划
     *
     * @param saleChance 要查找开发计划的销售机会
     * @return 受影响的行数
     */
    @RequestMapping("/getSalePlanListBySaleChanceId")
    List<SalePlan> getSalePlanListBySaleChanceId(@RequestBody SaleChance saleChance) {
        return salePlanService.getSalePlanListBySaleChanceId(saleChance);
    }

}
